
from math import sqrt

def get_primes_under(max):
    primes = []
    for i in xrange(3, max, 2):
        is_prime = True
        max_factor = sqrt(i)
        for p in primes:
            if p > max_factor:
                break
            if i % p == 0:
                is_prime = False
                break
        if is_prime:
            primes.append(i)
    return [2] + primes

primes = get_primes_under(1000000)
print 'there are %d primes under %d' % (len(primes), 1000000)
print primes[:100]

def enum_circular(primes):
    for p in primes:
        sp = str(p)
        l = len(sp)
        if l == 1:
            yield p
        else:
            is_circular = True
            for i in xrange(1, l):
                c = int(sp[i:] + sp[:i])
                if c not in primes:
                    is_circular = False
                    break
            if is_circular:
                yield p

count = 0
for c in enum_circular(primes):
    count += 1
    print c
print 'count: %d' % count